package cn.org.cute.ebocy.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 读取openapi.prop的处理类
 * @Copyright©2011, 2011 EBO CHANGYOU Co.td. All rights reserved.
 * @Version 1.0.0
 * @since jdk1.6
 * @author xiaolei
 * @date 2011-12-6
 */
public class Config {
	private final static String CONFIG_PATH="apiconfig/";
	private final static String CONFIG_FILE="apiconfig/apiconfig.prop";
	private final static String encoding = "utf-8";
	
	//apiconfig.prop 属性
	private static Map<String,String> propertValues = new HashMap<String,String>();
	
	static{	
		try{
		Properties propsub = new Properties();
		InputStream is = Config.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
		if(is == null){
			throw new Exception("file "+CONFIG_FILE+" is not found!");
		}		
		propsub.load(is);
		for(Object obj:propsub.keySet()){
			propertValues.put(obj.toString(), propsub.getProperty(obj.toString()));
		}
		
		String PLATFORM_NAME = getValue("PLATFORM_NAME","");
		if(!PLATFORM_NAME.equals("")){ // 加载子资源文件
			Properties props = new Properties();
			
			String fileName = CONFIG_PATH+"apiconfig_"+PLATFORM_NAME.toLowerCase()+".prop";
			InputStream subIS = Config.class.getClassLoader().getResourceAsStream(fileName);
			
			if(subIS == null){
				throw new Exception("file classes" + fileName + " is not found!");
			}
			props.load(subIS);
			
			for(Object obj:props.keySet()){
				propertValues.put(obj.toString(), props.getProperty(obj.toString()));
			}
		}				
		}catch(Exception e){
			System.err.println(e.getMessage());
			
		}
	}
	
	
	
	
	/**
	 * 获取配置文件key的值，如果不存在改属性则返回defaultValue
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getValue(String key,String defaultValue){
		String value = propertValues.get(key);
		if(value == null){
			value = defaultValue;
		}
		return value;
	}
	
	
	
	
	/**
	 * 获得Properties 对象的key对应的value
	 * @param pro  Properties 对象
	 * @param key key
	 * @return
	 */
	public  static String getProperty(Properties pro,String key){
		String value = pro.getProperty(key);
		if(value != null){
			try {
				value =	new String(value.getBytes("ISO8859-1"),encoding);
				return value;
			} catch (Exception e) {
				//TODO
			}
		}
		return value;		
	}
	
	
	
	/**
	 * 平台名称
	 * @return
	 */
	public static String getPlatformName(){
		return getValue("PLATFORM_NAME","");
	}
	
	
	/**
	 * 是否DEBUG
	 * @return
	 */
	public static boolean deBug(){
		String value = getValue("DEBUG","false");
		return "true".equalsIgnoreCase(value);
	}
	
	
	/**
	 * 获得平台名称,一般不用设置只有像腾讯多个平台的需要指明
	 * @return
	 */
	public static String getPfName(){
		return getValue("PF_NAME","");
	}
	
	/**
	 * 获得配置的APP_ID
	 * @return
	 */
	public static String getAppId(){
		return getValue("APP_ID","");
	}
	
	
	/**
	 * 获得配置的API_KEY
	 * @return
	 */
	public static String getApiKey(){
		return getValue("API_KEY","");
	}
	
	
	/**
	 * 获得配置的SECRET_KEY
	 * @return
	 */
	public static String getSecretKey(){
		return getValue("SECRET_KEY","");
	}
	
	/**
	 * 获得配置的LOG_FILE_PATH
	 * @return
	 */
	public static String getLogFilePath(){
		return getValue("LOG_FILE_PATH","");
	}
	
	
	/**
	 * 获得配置的API_SERVER_ADDR
	 * @return
	 */
	public static String getApiServerAddr(){
		return getValue("API_SERVER_ADDR","");
	}
	
	/**
	 * 获得配置的TEST_API_SERVER_ADDR
	 * @return
	 */
	public static String getTestApiServerAddr(){
		return getValue("TEST_API_SERVER_ADDR","");
	}
	
	
	/**
	 *是否初始化数据
	 * @return
	 */
	public static boolean getIfDataInit(){
		String IF_DATA_INIT = getValue("IF_DATA_INIT","");
		return "true".equalsIgnoreCase(IF_DATA_INIT);
	}
	
	
	/**
	 *获得语言类型
	 * @return
	 */
	public static String getLanTyep(){
		return getValue("LAN_TYPE","zh_cn");
	}
	
	
	/**
	 *游戏地址
	 * @return
	 */
	public static String getGameUrl(){
		return getValue("GAME_URL","");
	}
	
	
	/**
	 *host URL
	 * @return
	 */
	public static String getHostUrl(){
		return getValue("HOST_URL","");
	}
	
	
	/**
	 *IMG_HOST_URL
	 * @return
	 */
	public static String getImagesHostUrl(){
		return getValue("IMG_HOST_URL","");
	}
	
	public static void main(String[] args) {
		System.err.println("当前应用名称："+Config.getPlatformName());
	}
	
	
	
}
